<template>
  <view>
      <view v-for="(item,i) in goodsList" :key="i" @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求传参
        queryobj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        //商品列表
        goodsList: [],
        //商品总数，分页
        total: 0,
        //节流阀
        isLoading:false,

      };
    },
    onLoad(options) {
      this.queryobj.query = options.query || ''
      this.queryobj.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        this.isLoading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryobj)
        this.isLoading =false
        cb && cb()
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
        console.log(res);
      },
      gotoDetail(item){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id=" + item.goods_id
        })
      }
    },
    //上拉触底事件
    onReachBottom() {
      // 避免多余的请求
      if(this.queryobj.pagenum * this.queryobj.pagesize >= this.total) return uni.$showMsg('数据已经加载完毕！')
      //触底节流
      if(this.isLoading) return
      
      this.queryobj.pagenum++
      this.getGoodsList()
    },
    //下拉刷新事件
    onPullDownRefresh() {
      this.goodsList = []
      this.queryobj.pagenum = 1
      this.total = 0
      this.isLoading = false
      this.getGoodsList(()=>uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>